<!DOCTYPE html>
<html lang="zh-cn">
<head>
   
    <link type="text/css" rel="stylesheet" href="/bundles/blog-common.css?v=KOZafwuaDasEedEenI5aTy8aXH0epbm6VUJ0v3vsT_Q1"/>
<link id="MainCss" type="text/css" rel="stylesheet" href="/skins/ThinkInside/bundle-ThinkInside.css?v=RRjf6pEarGnbXZ86qxNycPfQivwSKWRa4heYLB15rVE1"/>
<link type="text/css" rel="stylesheet" href="/blog/customcss/428549.css?v=%2fam3bBTkW5NBWhBE%2fD0lcyJv5UM%3d"/>

</head>
<body>
<a name="top"></a>

<div id="page_begin_html"></div><script>load_page_begin_html();</script>

<div id="topics">
	<div class = "post">
		<h1 class = "postTitle">
			<a id="cb_post_title_url" class="postTitle2" href="https://www.cnblogs.com/frankdeng/p/9536450.html">Phoenix的安装使用与SQL查询HBase</a>
		</h1>
		<div class="clear"></div>
		<div class="postBody">
			<div id="cnblogs_post_body" class="blogpost-body"><h2>一. Phoenix的简介</h2>
<h3 id="1什么是phoenix">1. 什么是phoenix</h3>
<p>&nbsp; &nbsp; &nbsp; 现有hbase的查询工具有很多如：Hive，Tez，Impala，Shark/Spark，Phoenix等。今天主要说Phoenix。phoenix是一个在hbase上面实现的基于hadoop的OLTP技术，具有低延迟、事务性、可使用sql、提供jdbc接口的特点。&nbsp;而且phoenix还提供了hbase二级索引的解决方案，丰富了hbase查询的多样性，继承了hbase海量数据快速随机查询的特点。但是在生产环境中，不可以用在OLTP中。在线事务处理的环境中，需要低延迟，而Phoenix在查询HBase时，虽然做了一些优化，但延迟还是不小。所以依然是用在OLAT中，再将结果返回存储下来。</p>
<p>&nbsp; &nbsp; &nbsp; Phoenix完全使用Java编写，作为HBase内嵌的JDBC驱动。Phoenix查询引擎会将SQL查询转换为一个或多个HBase扫描，并编排执行以生成标准的JDBC结果集。直接使用HBase API、协同处理器与自定义过滤器，对于简单查询来说，其性能量级是毫秒，对于百万级别的行数来说，其性能量级是秒。</p>
<h3 id="1什么是phoenix">2. phoenix的架构</h3>
<p><img src="https://images2018.cnblogs.com/blog/1385722/201808/1385722-20180821153645305-1762766162.png" alt="" /></p>
<h3 id="1什么是phoenix">3. phoenix的下载</h3>
<p>选择对应的hbase版本进行下载，我这里使用的是hbase-1.2.6版本，下载地址：<a href="http://mirror.bit.edu.cn/apache/phoenix/" target="_blank">http://mirror.bit.edu.cn/apache/phoenix/</a></p>
<p><img src="https://images2018.cnblogs.com/blog/1385722/201808/1385722-20180820113606685-1740629478.png" alt="" /></p>
<p><img src="https://images2018.cnblogs.com/blog/1385722/201808/1385722-20180821123954661-634192241.png" alt="" /></p>
<div class="cnblogs_code">
<pre>[admin@node21 ~]$ cd /opt/software/<span>
[admin@node21 software]$ wget http://mirror.bit.edu.cn/apache/phoenix/apache-phoenix-4.14.0-HBase-1.2/bin/apache-phoenix-4.14.0-HBase-1.2-bin.tar.gz
<span>[admin@node21 software]$ ll
-rw-rw-r-- 1 admin admin 346171547 Jun&nbsp; 5 06:16 apache-phoenix-4.14.0-HBase-1.2-bin.tar.gz</span></span></pre>
</div>
<h3>4. 集群部署</h3>
<table style="background-color: #35c99d;" border="1">
<tbody>
<tr>
<td style="text-align: center;">节点IP</td>
<td>&nbsp;节点名称</td>
<td>HBase</td>
<td>Phoniex</td>
<td>Zookeeper</td>
<td>Hadoop</td>
</tr>
<tr>
<td>192.168.100.21</td>
<td style="text-align: center;">node21</td>
<td style="text-align: center;">&radic;</td>
<td style="text-align: center;">&nbsp;&radic;</td>
<td style="text-align: center;">&radic;</td>
<td style="text-align: center;">&nbsp;&radic;</td>
</tr>
<tr>
<td>192.168.100.22</td>
<td style="text-align: center;">node22</td>
<td style="text-align: center;">&nbsp;&radic;</td>
<td style="text-align: center;">&nbsp;</td>
<td style="text-align: center;">&radic;</td>
<td style="text-align: center;">&nbsp;&radic;</td>
</tr>
<tr>
<td>192.168.100.23</td>
<td style="text-align: center;">node23</td>
<td style="text-align: center;">&nbsp;&radic;</td>
<td style="text-align: center;">&nbsp;</td>
<td style="text-align: center;">&radic;</td>
<td style="text-align: center;">&nbsp;&radic;</td>
</tr>
</tbody>
</table>
<p>Hadoop集群搭建参考：&nbsp; &nbsp;&nbsp;<a id="post_title_link_9047698" href="https://www.cnblogs.com/frankdeng/p/9047698.html" target="_blank">CentOS7.5搭建Hadoop2.7.6完全分布式集群</a></p>
<p>zookeeper集群搭建参考：<a id="post_title_link_9018177" href="https://www.cnblogs.com/frankdeng/p/9018177.html" target="_blank">CentOS7.5搭建Zookeeper3.4.12集群</a></p>
<p>HBase集群搭建参考：&nbsp; &nbsp; &nbsp;&nbsp;<a id="cb_post_title_url" class="postTitle2" href="https://www.cnblogs.com/frankdeng/p/9310191.html">CentOS7.5搭建HBase1.2.6HA集群</a></p>
<h2>二. Phoenix服务端安装</h2>
<p>官网安装文档参考：<a href="http://phoenix.apache.org/installation.html" target="_blank">http://phoenix.apache.org/installation.html</a></p>
<h3>1. 解压缩安装包</h3>
<div class="cnblogs_code">
<pre>[admin@node21 software]$ tar zxvf apache-phoenix-4.14.0-HBase-1.2-bin.tar.gz -C /opt/module/<span>
[admin@node21 software]$ cd /opt/module/<span>
[admin@node21 module]$ mv apache-phoenix-4.14.0-HBase-1.2-bin phoenix-4.14.0-HBase-1.2<span>
[admin@node21 module]$ ll
drwxr-xr-x  5 admin admin   4096 Jun  5 05:41 phoenix-4.14.0-HBase-1.2</span></span></span></pre>
</div>
<h3>2. 拷贝jar包到hbase/lib包</h3>
<p>进入到phoenix的安装目录把phoenix-4.12.0-HBase-1.2-server.jar 拷贝到集群中每个节点( 主节点也要拷贝 )的 hbase 的 lib 目录下</p>
<div class="cnblogs_code">
<pre>[admin@node21 module]$ cd phoenix-4.14.0-HBase-1.2/<span>
[admin@node21 phoenix-4.14.0-HBase-1.2]$ cp phoenix-4.14.0-HBase-1.2-server.jar /opt/module/hbase-1.2.6/lib/<span>
[admin@node21 phoenix-4.14.0-HBase-1.2]$ scp phoenix-4.14.0-HBase-1.2-server.jar admin@node22:/opt/module/hbase-1.2.6/lib/<span>
[admin@node21 phoenix-4.14.0-HBase-1.2]$ scp phoenix-4.14.0-HBase-1.2-server.jar admin@node23:/opt/module/hbase-1.2.6/lib/<span><br /></span></span></span></span></pre>
</div>
<h3>3. 配置phoenix环境变量</h3>
<div class="cnblogs_code">
<pre>[admin@node21 phoenix-4.14.0-HBase-1.2]$ sudo vi /etc/profile
export PHOENIX_HOME=/opt/module/phoenix-4.14.0-HBase-1.2
export PATH=$PATH:$PHOENIX_HOME/bin
[admin@node21 phoenix-4.14.0-HBase-1.2]$ source /etc/profile</pre>
</div>
<h3>4. 重启Hbase</h3>
<div class="cnblogs_code">
<pre><span>                $ zkServer.sh start
[admin@node21 ~]$ start-<span>dfs.sh
[admin@node22 ~]$ start-<span>yarn.sh 
[admin@node21 ~]$ start-hbase.sh</span></span></span></pre>
</div>
<h3>5. 启动phoenix</h3>
<p>&nbsp;启动命令：phoenix-4.14.0-HBase-1.2/bin/sqlline.py&nbsp; zookeeper</p>
<div class="cnblogs_code">
<pre>[admin@node21 phoenix-4.14.0-HBase-1.2]$ ./bin/sqlline.py 192.168.100.21,192.168.100.22,192.168.100.23:2181<span>
Setting property: [incremental, false<span>]
Setting property: [isolation, TRANSACTION_READ_COMMITTED]
issuing: !connect jdbc:phoenix:192.168.100.21,192.168.100.22,192.168.100.23:2181<span> none none org.apache.phoenix.jdbc.PhoenixDriver
Connecting to jdbc:phoenix:192.168.100.21,192.168.100.22,192.168.100.23:2181<span>
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/opt/module/phoenix-4.14.0-HBase-1.2/phoenix-4.14.0-HBase-1.2-client.jar!/org/slf4j/impl/StaticLoggerBinder.class<span>]
SLF4J: Found binding in [jar:file:/opt/module/hadoop-2.7.6/share/hadoop/common/lib/slf4j-log4j12-1.7.10.jar!/org/slf4j/impl/StaticLoggerBinder.class<span>]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
18/08/21 02:28:30 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where<span> applicable
Connected to: Phoenix (version 4.14<span>)
Driver: PhoenixEmbeddedDriver (version 4.14<span>)
Autocommit status: true<span>
Transaction isolation: TRANSACTION_READ_COMMITTED
Building list of tables and columns for tab-completion (set fastconnect to true<span> to skip)...
133/133 (100%<span>) Done
Done
sqlline version 1.2.0
0: jdbc:phoenix:192.168.100.21,192.168.100.22&gt; </span></span></span></span></span></span></span></span></span></span></span></span></pre>
</div>
<h3>6. 验证phoenix</h3>
<p>1）查看所有表</p>
<p>输入&nbsp;<span style="color: #ff0000;">!tables</span>&nbsp;，查看都有哪些表。以下显示均为Phoenix系统表，系统表中维护了用户表的元数据信息。</p>
<p><img src="https://images2018.cnblogs.com/blog/1385722/201808/1385722-20180821150157678-1966771917.png" alt="" /></p>
<div>注意：上图中，我们使用了 sqlline.py 支持的 table 命令，该命令可以列出 HBase 中所有的表。这里需要注意 Phoenix 不支持直接显示 HBase Shell 中创建的表格。原因很简单，当在 Phoenix 创建一张表时，Phoenix 是将表进行了重组装。而对 HBase Shell 创建的表 Phoenix 并未进行加工，所以无法直接显示。如果需要将 HBase Shell 中创建的表格关联到 Phoenix 中查看，就需要在 Phoenix 中创建一个视图（View）做关联。</div>
<p>2）退出Phoenix</p>
<p>输入<span style="color: #ff0000;">&nbsp;!exit</span>&nbsp;命令(PS：Phoenix早期版本如(2.11版本)需输入!quilt才可退出，目前高版本已改为!exit命令)</p>
<p><img src="https://images2018.cnblogs.com/blog/1385722/201808/1385722-20180821152403490-1723150396.png" alt="" /></p>
<h2>三. Phoenix基本shell命令</h2>
<p>提示：以下，可能有部分命令在Phoenix更高版本中已失效，改为其他命令代替，请注意。</p>
<div class="cnblogs_code">
<pre>0: jdbc:phoenix:192.168.100.21,192.168.100.22&gt; help
!all                Execute the specified SQL against all the current
                    connections
!autocommit         Set autocommit mode on or off
!batch              Start or execute a batch of statements
!brief              Set verbose mode off
!call               Execute a callable statement
!close              Close the current connection to the database
!closeall           Close all current open connections
!columns            List all the columns for the specified table
!commit             Commit the current transaction (if autocommit is off)
!connect            Open a new connection to the database.
!dbinfo             Give metadata information about the database
!describe           Describe a table
!dropall            Drop all tables in the current database
!exportedkeys       List all the exported keys for the specified table
!go                 Select the current connection
!help               Print a summary of command usage
!history            Display the command history
!importedkeys       List all the imported keys for the specified table
!indexes            List all the indexes for the specified table
!isolation          Set the transaction isolation for this connection
!list               List the current connections
!manual             Display the SQLLine manual
!metadata           Obtain metadata information
!nativesql          Show the native SQL for the specified statement
!outputformat       Set the output format for displaying results
                    (table,vertical,csv,tsv,xmlattrs,xmlelements)
!primarykeys        List all the primary keys for the specified table
!procedures         List all the procedures
!properties         Connect to the database specified in the properties file(s)
!quit               Exits the program
!reconnect          Reconnect to the database
!record             Record all output to the specified file
!rehash             Fetch table and column names for command completion
!rollback           Roll back the current transaction (if autocommit is off)
!run                Run a script from the specified file
!save               Save the current variabes and aliases
!scan               Scan for installed JDBC drivers
!script             Start saving a script to a file
!set                Set a sqlline variable

Variable        Value      Description
=============== ========== ================================
autoCommit      true/false Enable/disable automatic
                           transaction commit
autoSave        true/false Automatically save preferences
color           true/false Control whether color is used
                           for display
fastConnect     true/false Skip building table/column list
                           for tab-completion
force           true/false Continue running script even
                           after errors
headerInterval  integer    The interval between which
                           headers are displayed
historyFile     path       File in which to save command
                           history. Default is
                           $HOME/.sqlline/history (UNIX,
                           Linux, Mac OS),
                           $HOME/sqlline/history (Windows)
incremental     true/false Do not receive all rows from
                           server before printing the first
                           row. Uses fewer resources,
                           especially for long-running
                           queries, but column widths may
                           be incorrect.
isolation       LEVEL      Set transaction isolation level
maxColumnWidth  integer    The maximum width to use when
                           displaying columns
maxHeight       integer    The maximum height of the
                           terminal
maxWidth        integer    The maximum width of the
                           terminal
numberFormat    pattern    Format numbers using
                           DecimalFormat pattern
outputFormat    table/vertical/csv/tsv Format mode for
                           result display
propertiesFile  path       File from which SqlLine reads
                           properties on startup; default is
                           $HOME/.sqlline/sqlline.properties
                           (UNIX, Linux, Mac OS),
                           $HOME/sqlline/sqlline.properties
                           (Windows)
rowLimit        integer    Maximum number of rows returned
                           from a query; zero means no
                           limit
showElapsedTime true/false Display execution time when
                           verbose
showHeader      true/false Show column names in query
                           results
showNestedErrs  true/false Display nested errors
showWarnings    true/false Display connection warnings
silent          true/false Be more silent
timeout         integer    Query timeout in seconds; less
                           than zero means no timeout
trimScripts     true/false Remove trailing spaces from
                           lines read from script files
verbose         true/false Show verbose error messages and
                           debug info
!sql                Execute a SQL command
!tables             List all the tables in the database
!typeinfo           Display the type map for the current connection
!verbose            Set verbose mode on

Comments, bug reports, and patches go to ???</pre>
<p>&nbsp;</p>
</div>
<h2>四.&nbsp;SQuirrel客户端安装</h2>
<p>&nbsp; &nbsp; &nbsp; 如果您更愿意使用客户端GUI与Phoenix进行交互，请下载并安装<a class="externalLink" href="http://squirrel-sql.sourceforge.net/">SQuirrel</a>。由于Phoenix是一个JDBC驱动程序，因此与此类工具的集成是无缝的。通过SQuirrel，您可以在SQL选项卡中发出SQL语句（创建表，插入数据，运行查询），并在&ldquo;对象&rdquo;选项卡中检查表元数据（即列表，列，主键和类型）。</p>
<h3>1. 下载SQuirrel</h3>
<p>官网下载地址：<a href="http://squirrel-sql.sourceforge.net/" target="_blank">http://squirrel-sql.sourceforge.net/</a></p>
<p><img src="https://images2018.cnblogs.com/blog/1385722/201808/1385722-20180821162513678-565392598.png" alt="" /></p>
<p><img src="https://images2018.cnblogs.com/blog/1385722/201808/1385722-20180821162631106-1538718689.png" alt="" /></p>
<p>下载完成：<img src="https://images2018.cnblogs.com/blog/1385722/201808/1385722-20180821163806649-1996693808.png" alt="" /></p>
<h3>2. 安装Squirrel</h3>
<p>进入到squirrel-sql-3.8.1-standard.jar所在目录，cmd，执行如下命令：</p>
<div class="cnblogs_code">
<pre>java -jar squirrel-sql-3.8.1-standard.jar</pre>
</div>
<p>然后，除了选择安装路径，其它的可按需选择，否则一路&ldquo;next&rdquo;即可。</p>
<h3>3. 配置Squirrel客户端</h3>
<p>（1）将服务器上Phoenix安装目录下的phoenix-4.14.0-HBase-1.2-client.jar文件，下载到Windows下squirrel-sql安装目录的lib文件夹下，并点击squirrel-sql.bat启动。</p>
<p>（2）添加新的驱动(Drivers -&gt; New Driver)，如下图所示：</p>
<p><img src="https://img-blog.csdn.net/20170904171058172?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvTkRGOTIz/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" /></p>
<p>（3）在弹出的&ldquo;Add Driver&rdquo;窗口中，设置Driver的&ldquo;Name&rdquo;为：Phoenix，&ldquo;Phoenix&rdquo;为：jdbc:phoenix:192.168.100.21，其中，192.168.100.21为你Phoenix服务器的地址；&ldquo;ClassName&rdquo;为：org.apache.phoenix.jdbc.PhoenixDriver，最终结果如下图：</p>
<p><img src="https://images2018.cnblogs.com/blog/1385722/201808/1385722-20180821170330991-563661757.png" alt="" /></p>
<p>参数说明：<br />Name：phoenix（可以填写其他名称）&nbsp;<br />ExampleURL：填写HBase集群的zookeeper集群的连接信息，这里是jdbc:phoenix:192.168.100.21:2181&nbsp;<br />Website URL：这里不填写&nbsp;<br />Java Class Path：选择phoenix-4.2.2-client.jar（是否选中无所谓，只要在Java Class Path里面就行）&nbsp;<br />Class Name：org.apache.phoenix.jdbc.PhoenixDriver</p>
<p>设置完成后，点击OK关闭。</p>
<p>（4）点击&ldquo;Drivers&rdquo;边上的&ldquo;Alias&rdquo;选项卡，和第（2）步的新建驱动一样新建Alias&nbsp;(Aliases -&gt; New Aliases)，在弹出的窗口中，填写Name，UserName，Password，这三项可任意填写，在Driver选项的下拉列表中查找我们刚添加的Phoenix驱动，然后点击&ldquo;Test&rdquo;按钮测试连接，然后点击&ldquo;Connect&rdquo;按钮，弹出&ldquo;Connection successful&rdquo;表示配置成功。结果如下图：</p>
<p><img src="https://images2018.cnblogs.com/blog/1385722/201808/1385722-20180821170705250-1154108637.png" alt="" /><img src="https://images2018.cnblogs.com/blog/1385722/201808/1385722-20180821170817335-722195004.png" alt="" /></p>
<p>参数说明：<br />Name：phoenix（可以填写其他名称）&nbsp;<br />Driver：选择phoenix，即上一步创建的Driver&nbsp;<br />URL：这里就是HBase集群的zookeeper的连接ip和port&nbsp;<br />User Name：随便填写&nbsp;<br />Password：随便填写</p>
<p>点击OK按钮，关闭弹出窗口。</p>
<p>（5）双击新建的test&nbsp;Alias ，然后点击&ldquo;Connect&rdquo;按钮，打开SQL命令行，在&ldquo;SQL&rdquo;选项卡下，即可编写sql命令，新建一张person表进行测试</p>
<p><img src="https://images2018.cnblogs.com/blog/1385722/201808/1385722-20180821171235846-969647252.png" alt="" /></p>
<div class="cnblogs_code">
<pre>create table person (id integer not null primary key,name varchar,age integer)</pre>
</div>
<pre><code class="language-html hljs xml"><img src="https://images2018.cnblogs.com/blog/1385722/201808/1385722-20180821171523441-493422395.png" alt="" /></code></pre>
<p>插入一条数据：</p>
<div class="cnblogs_code">
<pre>upsert into person values (1,'zhangsan' ,18)</pre>
</div>
<p>查询数据：</p>
<div class="cnblogs_code">
<pre>select * from person</pre>
</div>
<p>查询结果如下图所示：</p>
<p><img src="https://images2018.cnblogs.com/blog/1385722/201808/1385722-20180821172159625-1251948169.png" alt="" /></p>
<p>（6）到Hbase Shell下去查看我们刚刚添加的数据</p>
<p><img src="https://images2018.cnblogs.com/blog/1385722/201808/1385722-20180821172737676-550523589.png" alt="" /></p>
<p>可以看到我们新建的person表已经添加进去了，至此squirrel也安装并配置成功了！！</p>
<p>注意：通过squirrel添加的表，会自动转换为大写，由于Phoenix是区分大小写的，所以在Hbase Shell下，表名都要用大写，如：desc 'PERSON'，如果用desc 'person'是会报错的。</p>
<h3>4.关联hbase已创建的视图表</h3>
<p>创建视图表关联hbase;<br />create view "person" (<br />    pk varchar primary key,<br />    "info"."name" varchar,<br />    "info"."age"  varchar,<br />    "info"."birthday" varchar,<br />    "info"."company" varchar,<br />    "address"."contry" varchar,<br />    "address"."province" varchar,<br />    "address"."city" varchar<br />) as select * from "person";</p>
<p>查询表<br />select * from "person";</p>
<p>删除视图表</p>
<p>drop view "person";</p>
<h2>五. Phoenix的使用</h2>
<h3>1.&nbsp; 命令行方式</h3>
<p>&nbsp;从命令行执行SQL的终端接口现在与Phoenix捆绑在一起。要启动它，请从bin目录执行以下命令：</p>
<div class="cnblogs_code">
<pre>[admin@node21 phoenix-4.14.0-HBase-1.2]$ ./bin/sqlline.py 192.168.100.21,192.168.100.22,192.168.100.23:2181</pre>
</div>
<h4><a><span>1）</span></a><span style="color: #ff0000;">建表插入数据</span>：这里演示官方案例，安装包目录下examples包下STOCK_SYMBOL.sql 内容如下：</h4>
<div class="cnblogs_code">
<pre><span>CREATE TABLE IF NOT EXISTS STOCK_SYMBOL (SYMBOL VARCHAR NOT NULL PRIMARY KEY, COMPANY VARCHAR);
UPSERT INTO STOCK_SYMBOL VALUES ('CRM','SalesForce.com'<span>);
SELECT * FROM STOCK_SYMBOL;</span></span></pre>
</div>
<p>要从命令行执行SQL脚本，可以包含如下所示的SQL文件参数：</p>
<div class="cnblogs_code">
<pre>[admin@node21 phoenix-4.14.0-HBase-1.2]$ sqlline.py 192.168.100.21,192.168.100.22,192.168.100.23:2181 ./examples/<span>STOCK_SYMBOL.sql 
Setting property: [incremental, false<span>]
Setting property: [isolation, TRANSACTION_READ_COMMITTED]
Setting property: [run, ./examples/<span>STOCK_SYMBOL.sql]
issuing: !connect jdbc:phoenix:192.168.100.21,192.168.100.22,192.168.100.23:2181<span> none none org.apache.phoenix.jdbc.PhoenixDriver
Connecting to jdbc:phoenix:192.168.100.21,192.168.100.22,192.168.100.23:2181<span>
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/opt/module/phoenix-4.14.0-HBase-1.2/phoenix-4.14.0-HBase-1.2-client.jar!/org/slf4j/impl/StaticLoggerBinder.class<span>]
SLF4J: Found binding in [jar:file:/opt/module/hadoop-2.7.6/share/hadoop/common/lib/slf4j-log4j12-1.7.10.jar!/org/slf4j/impl/StaticLoggerBinder.class<span>]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
18/08/21 07:53:36 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where<span> applicable
Connected to: Phoenix (version 4.14<span>)
Driver: PhoenixEmbeddedDriver (version 4.14<span>)
Autocommit status: true<span>
Transaction isolation: TRANSACTION_READ_COMMITTED
Building list of tables and columns for tab-completion (set fastconnect to true<span> to skip)...
136/136 (100%<span>) Done
Done
1/4<span>          CREATE TABLE IF NOT EXISTS STOCK_SYMBOL (SYMBOL VARCHAR NOT NULL PRIMARY KEY, COMPANY VARCHAR);
No rows affected (3.818<span> seconds)
2/4          UPSERT INTO STOCK_SYMBOL VALUES ('CRM','SalesForce.com'<span>);
1 row affected (0.166<span> seconds)
3/4          SELECT *<span> FROM STOCK_SYMBOL;
+---------+-----------------+
| SYMBOL  |     COMPANY     |
+---------+-----------------+
| CRM     | SalesForce.com  |
+---------+-----------------+
1 row selected (0.184<span> seconds)
4/4<span>          
Closing: org.apache.phoenix.jdbc.PhoenixConnection
sqlline version 1.2.0</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></pre>
</div>
<p>examples/STOCK_SYMBOL.csv</p>
<div class="cnblogs_code">
<pre><span>AAPL,APPLE Inc.
CRM,SALESFORCE
GOOG,Google
HOG,Harlet-<span>Davidson Inc.
HPQ,Hewlett Packard
INTC,Intel
MSFT,Microsoft
WAG,Walgreens
WMT,Walmart</span></span></pre>
</div>
<h4><a><span>2）</span></a><span style="color: #ff0000;">导入数据</span>：此外，您可以使用bin / psql.py加载CSV数据或执行SQL脚本。例如：</h4>
<div class="cnblogs_code">
<pre>[admin@node21 phoenix-4.14.0-HBase-1.2]$ ./bin/psql.py -t STOCK_SYMBOL 192.168.100.21,192.168.100.22,192.168.100.23:2181 ./examples/STOCK_SYMBOL.csv </pre>
</div>
<p>PS：其中 -t 后面是表名， ../examples/STOCK_SYMBOL.csv是csv数据（注意数据的分隔符需要是逗号）。</p>
<p><img src="https://images2018.cnblogs.com/blog/1385722/201808/1385722-20180821202704749-1674960685.png" alt="" /></p>
<h4><a><span>3）</span></a><span style="color: #ff0000;">查询数据</span></h4>
<div class="cnblogs_code">
<pre>[admin@node21 phoenix-4.14.0-HBase-1.2]$ ./bin/sqlline.py 192.168.100.21,192.168.100.22,192.168.100.23:2181
0: jdbc:phoenix:192.168.100.21,192.168.100.22&gt; select * from STOCK_SYMBOL;</pre>
</div>
<p><img src="https://images2018.cnblogs.com/blog/1385722/201808/1385722-20180821203248744-998078738.png" alt="" /></p>
<p>查询总条数</p>
<p><img src="https://images2018.cnblogs.com/blog/1385722/201808/1385722-20180821203336044-234619886.png" alt="" /></p>
<h3>2.&nbsp;客户端方式</h3>
<p>&nbsp; SQuirrel是用来连接Phoenix的客户端，前面已做介绍。</p>
<h2>六.&nbsp;Phoenix的api操作HBase</h2>
<h3>1. 参考资料链接</h3>
<p>常见问题参考：<a href="http://phoenix.apache.org/faq.html#What_is_the_Phoenix_JDBC_URL_syntax" target="_blank">http://phoenix.apache.org/faq.html#What_is_the_Phoenix_JDBC_URL_syntax</a></p>
<p>帮助文档参考：<a href="https://help.aliyun.com/document_detail/69105.html?spm=a2c4g.11186623.6.591.420a48cbSK2wy8" target="_blank">https://help.aliyun.com/document_detail/69105.html?spm=a2c4g.11186623.6.591.420a48cbSK2wy8</a></p>
<p>参考文档:&nbsp;<a href="http://phoenix.apache.org/phoenix_spark.html" target="_blank">http://phoenix.apache.org/phoenix_spark.html</a></p>
<h3>2. 开发环境准备</h3>
<p>IDEA、jdk1.8、window10、hadoop2.7.6、hbase1.2.6、phoenix4.14.0</p>
<p>　　1、从集群拷贝以下文件：core-site.xml、hbase-site.xml、hdfs-site.xml文件放到工程src下</p>
<p>　　2、把phoenix的phoenix-4.14.0-client.jar和phoenix-core-4.14.0.jar添加到工程classpath</p>
<p>　　3、配置集群中各节点的hosts文件，把客户端的hostname：IP添加进去</p>
<p>　　4、在客户端host文件中加入集群的hostname和IP　　</p>
<h3><strong>3. 例一</strong></h3>
<div class="cnblogs_code">
<pre>package com.xyg.phoenix;<br /><br />import java.sql.Connection;<br />import java.sql.DriverManager;<br />import java.sql.ResultSet;<br />import java.sql.SQLException;<br />import java.sql.Statement;<br /><br />/**<br /> * Author: Mr.Deng<br /> * Date: 2018/8/25<br /> * Desc: create table, create index, insert data, select table.<br /> */<br />public class TestPhoenixJDBC {<br /><br />    private static String driver = "org.apache.phoenix.jdbc.PhoenixDriver";<br /><br />    public static void main(String[] args) throws SQLException {<br /><br />        Connection conn = null;<br />        Statement stmt = null;<br />        ResultSet rs = null;<br />        try {<br />              Class.forName(driver);<br />            } catch (ClassNotFoundException e) {<br />                e.printStackTrace();<br />            }<br />        conn = DriverManager.getConnection("jdbc:phoenix:node21,node22,node23:2181");<br />        stmt = conn.createStatement();<br />        stmt.execute("drop table if exists test");<br />        stmt.execute("create table test (mykey integer not null primary key, mycolumn varchar)");<br />        stmt.execute("create index test_idx on test(mycolumn)");<br />        stmt.executeUpdate("upsert into test values (1,'World!')");<br />        stmt.executeUpdate("upsert into test values (2,'Hello')");<br />        stmt.executeUpdate("upsert into test values (3,'World!')");<br />        conn.commit();<br />        rs = stmt.executeQuery("select mykey from test where mycolumn='Hello'");<br />        while (rs.next()) {<br />            System.out.println(rs.getInt(1));<br />        }<br />        stmt.close();<br />        rs.close();<br />        conn.close();<br /><br />    }<br /><br />}</pre>
</div></div>

</body>
</html>
